package love;

import lombok.extern.slf4j.Slf4j;

/**
 * @author: levi
 * @description: TODO
 * @date: 2022-9-5 10:18
 * @version: 1.0
 * 0 1 2 3 4 5
 * 0 1 1 2 3 5 8 13
 */
@Slf4j
public class Fibonacci {
    public static void main(String[] args) {
        java.util.Scanner s=new java.util.Scanner(System.in);
        System.out.println("请输入n的值：");
        int n =s.nextInt();
        System.out.println(fib1(n));
        System.out.println(fib2(n));
        log.info("测试一下输入{}","giao");
    }

    // 递归
    public static int fib1(int n){
        if(n < 0) throw new IllegalArgumentException("传参错误");
        if(n == 0 || n == 1) return n;

        return fib1(n -1) + fib1(n - 2);
    }

    // 迭代
    public static int fib2(int n){
        if(n < 0) throw new IllegalArgumentException("传参错误");
        if(n == 0 || n == 1) return n;

        int first = 0;
        int second = 1;
        for (int i = 0; i < n - 1; i++) {
            int sum = first + second;
            first = second;
            second = sum;
        }
        return second;
    }
}
